package chapter10;

import java.util.*;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/19 15:55
 */
enum Day {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}

class Worker {
    private String name;
    private Set<Day> availableDays;

    public Worker(String name, Set<Day> availableDays) {
        this.name = name;
        this.availableDays = availableDays;
    }

    public String getName() {
        return name;
    }

    public Set<Day> getAvailableDays() {
        return availableDays;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Worker worker = (Worker) o;
        return name.equals(worker.name) &&
                Objects.equals(availableDays, worker.availableDays);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, availableDays);
    }

    @Override
    public String toString() {
        return "Worker{" +
                "name='" + name + '\'' +
                ", availableDays=" + availableDays +
                '}';
    }
}

public class EnumSetTest {

    public static void main(String[] args) {
        Set<Day> weekend = EnumSet.noneOf(Day.class);
        weekend.add(Day.SATURDAY);
        weekend.add(Day.FRIDAY);

        System.out.println(weekend);

        Worker[] workers = new Worker[]{
                new Worker("张三", EnumSet.of(Day.MONDAY, Day.TUESDAY, Day.WEDNESDAY, Day.FRIDAY)),
                new Worker("李四", EnumSet.of(Day.TUESDAY, Day.THURSDAY, Day.SATURDAY)),
                new Worker("王五", EnumSet.of(Day.TUESDAY, Day.THURSDAY))
        };

        // 哪些天一个人都不回来?
        Set<Day> days = EnumSet.allOf(Day.class);
        for (Worker worker : workers) {
            days.removeAll(worker.getAvailableDays());
        }

        System.out.println(days);

        // 哪些天至少来一人?
        Set<Day> days1 = EnumSet.noneOf(Day.class);
        for (Worker worker : workers) {
            days1.addAll(worker.getAvailableDays());
        }
        System.out.println(days1);

        // 哪些天大家都回来?
        Set<Day> days2 = EnumSet.allOf(Day.class);
        for (Worker worker : workers) {
            days2.retainAll(worker.getAvailableDays());
        }

        System.out.println(days2);

        // 哪些员工会在周一和周二来?
        Set<Worker> availableWorkers = new HashSet<>();
        for (Worker worker : workers) {
            if (worker.getAvailableDays().containsAll(EnumSet.of(Day.MONDAY, Day.TUESDAY))) {
                availableWorkers.add(worker);
            }
        }
        System.out.println(availableWorkers);


        // 哪些天会至少有两个人来?
        Map<Day, Integer> countMap = new EnumMap<>(Day.class);
        for (Worker worker : workers) {
            for (Day availableDay : worker.getAvailableDays()) {
                Integer count = countMap.get(availableDay);
                countMap.put(availableDay, count == null ? 1 : count + 1);
            }
        }

        Set<Day> days4 = EnumSet.noneOf(Day.class);
        for (Map.Entry<Day, Integer> entry : countMap.entrySet()) {
            if (entry.getValue() >= 2) {
                days4.add(entry.getKey());
            }
        }

        System.out.println(days4);

    }
}
